// by Xeno
#define __DEBUG__
#define THIS_FILE "init.sqf"
diag_log [diag_frameno, diag_ticktime, time, "Executing Dom init.sqf"];

#include "x_setup.sqf"

// just a check to prevent init.sqf running more than once
// shouldn't happen, but we want to be sure :)
if (!isNil QGVAR(init_started)) exitWith {
	diag_log [diag_frameno, diag_ticktime, time, "Dom init.sqf executed more than once"];
};
GVAR(init_started) = true;

GVAR(IS_HC_CLIENT) = !isDedicated && {!hasInterface};

__TRACE_1("","d_IS_HC_CLIENT");

if (GVAR(IS_HC_CLIENT)) then {
	diag_log [QGVAR(IS_HC_CLIENT),GVAR(IS_HC_CLIENT),isNull player,name player];
	0 spawn {
		waituntil {!isnull player && player == player};
		sleep 1;
		[QGVAR(hcunit),player] call FUNC(NetCallEventCTS);
		[QGVAR(nHCObj),player] call FUNC(NetCallEventCTS);
		player allowDamage false;
		player addEventHandler ["HandleDamage",{0}];
	};
};

if (isDedicated) then {
	0 spawn {
		private ["_timeout"];
		_timeout = time + 180;
		waitUntil {!isNil "HC_D_UNIT" || {time > _timeout}};
		if (!isNil "HC_D_UNIT") then {
			waitUntil {!isNull HC_D_UNIT || {time > _timeout}};
			if (!isNull HC_D_UNIT) then {
				GVAR(HC_CLIENT_OBJ) = HC_D_UNIT;
				GVAR(HC_CLIENT_OBJ_NAME) = name HC_D_UNIT;
				__TRACE_2("","d_HC_CLIENT_OBJ","d_HC_CLIENT_OBJ_NAME");
			};
		};
	};
};

if (isMultiplayer && {!isDedicated}) then {
	enableRadio false;
	0 fadeSound 0;
	titleText ["", "BLACK FADED"];
};

enableSaving [false,false];
enableTeamSwitch false;
enableSentences false;

if (isServer) then {
	GVAR(domdatabase) = ("Arma2Net.Unmanaged" callExtension "DomDatabase ['Init','DomDatabase','Dom.db']") == "[]";
	publicVariable QGVAR(domdatabase);
	__TRACE_1("","d_domdatabase")
	if (GVAR(domdatabase)) then {
		GVAR(server_name) = "Arma2Net.Unmanaged" callExtension "DomDatabase ['Servername']";
		__TRACE_1("","d_server_name")
		publicVariable QGVAR(server_name);
	};
};

// process d_init in one frame, move to Alpha 3 post init once it works reliably?
GVAR(init_obj) = GVAR(HeliHEmpty) createVehicleLocal [0, 0, 0];
GVAR(init_obj) addEventHandler ["killed", {__ccppfln(d_init.sqf);deleteVehicle GVAR(init_obj);GVAR(init_obj) = nil}];
GVAR(init_obj) setDamage 1;

diag_log [diag_frameno, diag_ticktime, time, "Dom init.sqf processed"];

//TYRGHEN
0 spawn compile preprocessFileLineNumbers "tmf\init.sqf";